package com.cremamobile.filemanager;

import java.io.File;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.cremamobile.filemanager.treeview.InMemoryTreeStateManager;
import com.cremamobile.filemanager.treeview.TreeBuilder;
import com.cremamobile.filemanager.treeview.TreeStateManager;
import com.cremamobile.filemanager.treeview.TreeViewAdapter;
import com.cremamobile.filemanager.treeview.TreeViewAdapterParent;
import com.cremamobile.filemanager.treeview.TreeViewList;

class NavigationDrawerDirectoryTree extends NavigationDrawerTreeHelper{
    public TreeViewList listView;
    public TreeStateManager<Long> manager = null;
    public TreeViewAdapter listAdapter;
    public boolean collapsible;
    public int	treeLevel = 1;
    
    public int deviceCount = 0;

    public File root;

    public void createTreeManager(Activity parent, TreeViewAdapterParent<Long> treeParent, int type) {
		this.manager = new InMemoryTreeStateManager();
        this.manager.setTreeBuilder(new TreeBuilder<Long>(manager));

        this.listAdapter = new TreeViewAdapter(parent, treeParent, manager, treeLevel);
        this.listView.setAdapter(listAdapter);
        this.listView.setCollapsible(true);
        this.collapsible = true;
        this.tree_type = type;
    }

	@Override
	public int getPositionForPath(String path) {
		return this.listAdapter.getPositionForPath(path);
	}

}

class NavigationDrawerFavoriteTree extends NavigationDrawerTreeHelper {
	public ListView	listView;
	public ListViewAdapter listAdapter;
	public TreeViewAdapterParent<Long> treeParent;
	
	@Override
	void createTreeManager(Activity parent,
			TreeViewAdapterParent<Long> treeParent, int type) {
		// TODO Auto-generated method stub
		tree_type = type;
		this.listAdapter = new ListViewAdapter(parent);
		this.listView.setAdapter(listAdapter);
		this.treeParent = treeParent;
		//TODO...
		this.listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				gotoParent(position);
			}
		});
	}
	
	public void gotoParent(int position) {
		treeParent.onHandleNavigation(position, new Long(0), (String)listAdapter.getItem(position), false);
	}
	
	class ListViewAdapter extends BaseAdapter {
		List<String>	mFiles;
		Context	mContext;
		boolean mSelectMode;
		
		public ListViewAdapter(Context context) {
			mContext = context;
		}
		
		public ListViewAdapter(Context context, List<String> files) {
			mContext = context;
			mFiles = files;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (mFiles == null) return 0;
			return mFiles.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			if (mFiles == null) return null;
			return mFiles.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View grid;
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			if (convertView == null) {
				grid = new View(mContext);
				grid = inflater.inflate(R.layout.favorite_list_item, parent, false);
			} else {
				grid = convertView;
			}

//				if(position < chacheImgList.size())
//					imageView.setImageBitmap(chacheImgList.get(position));
			
			return updateView(grid, position);
		}

		private View updateView(View view, int position) {
			final String file = (String)getItem(position);
	        final int list_num = position;
			
			TextView fileNameView = (TextView) view.findViewById(R.id.file_name);
	        fileNameView.setText(mContext.getResources().getString(R.string.favorite_list, position+1, file));
	        
	        Button removeButton = (Button) view.findViewById(R.id.button_remove);
	        removeButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					treeParent.removeFavorite(file, list_num);
				}
	        	
	        });
			return view;
		}
		
		public void setList(List<String> files) {
			mFiles = null;
			mFiles = files;
			
			notifyDataSetChanged();
		}
		
		public void add(String name) {
			mFiles.add(name);
			
			notifyDataSetChanged();
		}

		public void remove(int index) {
			mFiles.remove(index);
			
			notifyDataSetChanged();		
		}
		
		public void remove(String path) {
			mFiles.remove(path);
			notifyDataSetChanged();		
		}
		
		public boolean isExist(String path) {
			return mFiles.contains(path);
		}
		
		public String toString() {
			return TextUtils.join(",", mFiles);
		}
		
		public int getPostion(String path) {
			if (mFiles == null)
				return NOT_SET;
			int count = mFiles.size();
			for (int i = 0; i < count; i++) {
				if (((String)(mFiles.get(i))).equals(path))
					return i;
			}
			return NOT_SET;
		}
	}

	@Override
	public int getPositionForPath(String path) {
		// TODO Auto-generated method stub
		return listAdapter.getPostion(path);
	}
}

public abstract class NavigationDrawerTreeHelper {
	public static final int TREE_TYPE_INTERNAL = 0;
	public static final int TREE_TYPE_EXTERNAL = 1;
	public static final int TREE_TYPE_FAVORITE = 2;
	
	public static final int NOT_SET = -1;
	
    public int tree_type;
    public long currentSelectedID = NOT_SET;
    public int current_position = NOT_SET;
    
    abstract void createTreeManager(Activity parent, TreeViewAdapterParent<Long> treeParent, int type);
    
    abstract int getPositionForPath(String path);
}
